package org.flyby.api.persistence;

import org.apache.commons.lang.StringUtils;

import javax.persistence.*;

@Entity
@Table(name = "user2")
@NamedQueries({
        @NamedQuery(name = "User.findByUserNameAndPassword",
                query = " SELECT u " +
                        " FROM User u " +
                        " WHERE u.userName =:userName " +
                        " AND u.password =:password ")
})
public class User implements BaseEntity {

    @Id
    @Access(AccessType.PROPERTY)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "USER_ID", nullable = false)
    private int id;

    @Column(name = "USER_PASS", length = 20, nullable = false)
    private String password;

    @Column(name = "USER_NAME", length = 20, nullable = false)
    private String userName;

    @Column(name = "FIRST_NAME", length = 22, nullable = false)
    private String firstName;

    @Column(name = "LAST_NAME", length = 22, nullable = false)
    private String lastName;

    public User() {
        super();
    }

    @Override
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstNameAndLastName() {
        return StringUtils.trimToEmpty(firstName) + " " + StringUtils.trimToEmpty(lastName);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (id != user.id) return false;
        if (password != null ? !password.equals(user.password) : user.password != null) return false;
        if (userName != null ? !userName.equals(user.userName) : user.userName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (userName != null ? userName.hashCode() : 0);
        return result;
    }
}
